#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        WaitforElementById(driver,"com.gionee.aora.market:id/main_title_download_img",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    while True:
        try:
            driver.find_element_by_id("com.gionee.aora.market:id/downloadManager_mainLay").click()
            try:
                WaitforElementById(driver,"com.gionee.aora.market:id/downloadManager_cancel_button",30).click()
            except NoSuchElementException:
                raise NoSuchElementException
            else:
                WaitforElementById(driver,"com.gionee.aora.market:id/btn_2",30).click()
                time.sleep(1)
        except:
            break
    driver.back()


def clickappdown(driver):
    try:
        tasks=WaitforElementsById(driver,'com.gionee.aora.market:id/mix_app_lay',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in tasks:
            try:
                size=WaitforElementById(btn,"com.gionee.aora.market:id/mix_app_packageSize",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-2])<20:
                    continue
            try:
                down_btn=WaitforElementById(btn,'com.gionee.aora.market:id/mix_app_button',30)
            except NoSuchElementException:
                continue
            else:
                down_btn.click()
                waittime=30
                while waittime:
                    try:
                        text=WaitforElementById(btn,'com.gionee.aora.market:id/download_present_tv',5).get_attribute("text")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0B/' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"

def game(driver):
    try:
        WaitforElementByName(driver,u"分类",30).click()
        WaitforElementByName(driver,u"游戏",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'益智',u'休闲',u'消除',u'捕鱼',u'音乐',u'卡牌',u'RPG网游',u'动作', \
                   u'武侠',u'策略',u'经营',u'斗地主',u'棋牌',u'麻将',u'桌游',u'德州扑克',u'纸牌',u'动作', \
                   u'跑酷',u'格斗',u'冒险',u'跳跃',u'闯关',u'即时',u'回合',u'魔幻',u'仙侠',u'塔防', \
                   u'战争',u'策略经营',u'攻略',u'飞机',u'射击',u'坦克',u'赛车',u'摩托',u'篮球',u'足球', \
                   u'台球',u'极限运动']
#    gamecatetory= [u'益智',u'休闲']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
    #driver.back()

def soft(driver):
    try:
        #WaitforElementByName(driver,u"分类",30).click()
        WaitforElementByName(driver,u"软件",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'视频',u'音乐',u'播放器',u'短视频',u'电台',u'直播', \
                   u'聊天',u'交友',u'通话',u'短信',u'婚恋',u'社区',  \
                   u'商城',u'团购',u'优惠',u'导购',u'海淘',u'生活',  \
                   u'天气',u'日历',u'美食',u'房产',u'娱乐',u'新闻',  \
                   u'摄像',u'相册',u'安全',u'省电',u'WIFI',u'垃圾清理',  \
                   u'银行',u'股票',u'理财',u'记账',u'支付',u'贷款',  \
                   u'学习',u'词典',u'英语',u'考试',u'驾考',u'旅游',  \
                   u'地图导航',u'出行',u'车票',u'航班',u'酒店',u'运动',  \
                   u'健身',u'减肥',u'健康',u'桌面',u'主题',u'锁屏',  \
                   u'壁纸',u'动态壁纸',u'办公',u'邮箱',u'笔记',u'名片',  \
                   u'育儿',u'孕期',u'儿歌',u'故事',u'益智',u'健康养生',  \
                   u'医疗',u'经期',u'用药',u'输入法',u'浏览器',u'文件管理',  \
                   u'小工具',u'计算器',u'闹钟',u'支付',u'银行',u'证券',  \
                   u'理财',u'彩票',u'记账', \
                   u'电子书',u'报刊杂志',u'笑话',u'百科',u'资讯',u'相机',u'照片美化']
#    gamecatetory= [u'视频',u'音乐',u'播放器']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
    driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE        # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.gionee.aora.market'    # apk的包名
    desired_caps['appActivity'] = '.gui.main.MainNewActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    del_appdown_task(driver)
    game(driver)
    soft(driver)
    driver.quit()

#run()
